/*
 *
 * Copyright (c) 2009 Cam Spiers (camspiers [at] gmail [dot] com)
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license
 *
 */

/**
 *
 * konami-code
 *
 * @name     konami-code
 * @version  0.2
 * @author   Cam Spiers (camspiers [at] gmail [dot] com)
 *
 * Listen for character sequences
 *
 */
"use strict";
/*global window, event*/
var konami = {

	configs : [],

	addConfig : function (config) {
		if (config.code !== undefined && config.code === 'konami') {
			config.code = '3838404037393739666513';
		}
		config.input = '';
		konami.configs.push(config);
		return konami;
	},

	listen : function (framework) {
		var input = '', listen = function (e) {
			input = e ? e.keyCode : event.keyCode;
			for (var i in konami.configs) {
				if (konami.configs.hasOwnProperty(i)) {
					konami.configs[i].input += input;
					if (konami.configs[i].code !== undefined) {
						if (konami.configs[i].code.indexOf(konami.configs[i].input) === 0) {
							if (konami.configs[i].code === konami.configs[i].input) {
								konami.configs[i].fn(e, input);
								konami.configs[i].input = '';
							}
						} else {
							konami.configs[i].input = '';
						}
					} else {
						konami.configs[i].fn(e, input);
						konami.configs[i].input = '';
					}
				}
			}

		};
		if (framework) {
			if (framework.fn.jquery !== undefined) {
				framework(window).keydown(listen);
			}
		} else {
			window.document.onkeydown = listen;
		}
		return konami;
	}
};